package com.cn.jinl.service;

import com.cn.jinl.api.IActionPermissionService;
import com.cn.jinl.dao.ActionPermissionMapper;
import com.cn.jinl.domain.ActionPermission;
import com.cn.jinl.domain.ActionPermissionExample;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.ConvertTree;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ActionPermissionServiceImpl implements IActionPermissionService {

    @Autowired
    private ActionPermissionMapper actionPermissionMapper;

    @Override
    public List<ActionPermission> queryActionPermission() throws Exception {
        ActionPermissionExample example = new ActionPermissionExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ActionPermission> list = actionPermissionMapper.selectByExample(example);
        if(CollectionsUtil.isNotEmpty(list)){
            list = ConvertTree.getTree(list,"id","pId");
        }
        return list;
    }

    @Override
    public Map<String, String> queryActionPermissionMap() throws Exception {
        ActionPermissionExample example = new ActionPermissionExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ActionPermission> list = actionPermissionMapper.selectByExample(example);
        Map<String, String> map = Maps.newHashMap();
        if(CollectionsUtil.isNotEmpty(list)){
            for(ActionPermission actionPermission:list){
                map.put(actionPermission.getPermission(),actionPermission.getModuleName());
            }
        }
        return map;
    }
}
